package com.dingqb.myapplication.paint;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Create by dingqb
 * On 2019-08-21
 * Description:
 */
public class WaveLineView extends View {
    private static final String TAG = "WaveLineView";

    public WaveLineView(Context context) {

        super(context);
    }

    public WaveLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        animator();
    }

    public WaveLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bejizer(canvas);
    }
    Paint paint = new Paint();
    Path path = new Path();
    //
    void bejizer(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.GREEN);
//        paint.setStrokeWidth(5);
        //贝吉哀曲线
        path.moveTo(100 + dx, 300);
        path.quadTo(200 + dx, 200, 300 + dx, 300);
        path.quadTo(400 + dx, 400, 500 + dx, 300);
        path.close();
        canvas.drawPath(path, paint);
        //普通折线
        Path path2 = new Path();
        paint.setColor(Color.BLUE);
        path2.moveTo(100, 300);
        path2.lineTo(200, 200);
        path2.lineTo(300, 300);
        path2.lineTo(400, 400);
        path2.lineTo(500, 300);
        // 关闭则 起始点连接起来，形成闭环。
//        path2.close();
//        canvas.drawPath(path2, paint);
        paint.setColor(Color.parseColor("#ff00ff"));
        canvas.drawLine(0, 200, getMeasuredWidth(), 200, paint);
        canvas.drawLine(0, 400, getMeasuredWidth(), 400, paint);
    }

    int dx;

    void animator() {
        ValueAnimator animator = ValueAnimator.ofInt(0, 10);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(5000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int v = (int) animation.getAnimatedValue();
                Log.e(TAG, "onAnimationUpdate: "+dx);
                //dx -= v;
                postInvalidate();
            }
        });
        animator.start();

    }
}
